package com.example.springboot.dao.impl;

import com.example.springboot.dao.EntityDao;
import com.example.springboot.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by wuning at 2018/8/22 11:19
 **/
@Repository
public class CourseDaoImpl implements EntityDao<Course> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Course getEntityById(Integer id) {
        List<Course> list = jdbcTemplate.query("select * from tb_course where id = ?", new Object[]{id}, new BeanPropertyRowMapper(Course.class));
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }

    }

    @Override
    public List<Course> getEntityList() {
        List<Course> list = jdbcTemplate.query("select * from tb_course", new Object[]{}, new BeanPropertyRowMapper(Course.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }
    }

    @Override
    public int add(Course course) {
        return jdbcTemplate.update("insert into tb_course(code,name, teacher) values(?, ?, ?)",
                course.getCode(),course.getName(), course.getTeacher());
    }

    @Override
    public int update(Integer id, Course course) {
        return jdbcTemplate.update("UPDATE tb_course SET code = ? ,name = ? , teacher = ? WHERE id = ?",
                course.getCode(),course.getName(), course.getTeacher(), id);
    }

    @Override
    public int delete(Integer id) {
        return jdbcTemplate.update("DELETE from tb_course where id = ? ",id);
    }
}
